#import "RNEditItemViewController.h"
#import "RNConstants.h"
#import "RNItem.h"
#import "RNListItem.h"
#import "SRTrackingTableView.h"
#import "RNEditItemBackgroundView.h"

@implementation RNEditItemViewController

@synthesize listItemsTableView = listItemsTableView_;
@synthesize titleView = titleView_;
@synthesize optionsView = optionsView_;
@synthesize listItemsController = listItemsController_;

-(void)awakeFromNib {
    [super awakeFromNib];
}

-(void)updateViewFrame_ {
    NSRect editViewFrame = [[self view] frame];
    CGFloat currentHeight = NSHeight(editViewFrame);
    CGFloat newHeight = [self preferredHeightForWidth:NSWidth(editViewFrame)];
    editViewFrame.origin.y -= newHeight - currentHeight;
    editViewFrame.size.height = newHeight;
    [[self view] setFrame:editViewFrame];
}

-(CGFloat)preferredHeightForWidth:(CGFloat)aWidth {
    NSUInteger itemCount = [[[self editingItem] listItems] count];
    return NSHeight([[self titleView] frame]) + NSHeight([[self optionsView] frame]) + (itemCount * [[self listItemsTableView] rowHeight]) + ([[self listItemsTableView] intercellSpacing].height * itemCount);
}

-(void)setEditingItem:(id)item {
    if (editingItem_)
        [editingItem_ removeObserver:self forKeyPath:@"listItems"];
    editingItem_ = item;
    if (item)
        [item addObserver:self forKeyPath:@"listItems" options:NSKeyValueObservingOptionNew context:NULL];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    if ([keyPath isEqualToString:@"listItems"])
        [self updateViewFrame_];
}

+(NSSet *)keyPathsForValuesAffectingListCountString {
    return [NSSet setWithObject:@"editingItem.listItems"];
}

-(NSString *)listCountString {
    NSInteger listCount = [[[self editingItem] listItems] count];
    return [NSString stringWithFormat:@"%i %@", listCount, listCount == 1 ? @"lists" : @"lists"];
}


#pragma mark -
#pragma mark Actions

-(IBAction)deleteListItemFromCancelButtonCell:(id)sender {
    RNListItem *itemToDelete = [[[self listItemsController] arrangedObjects] objectAtIndex:[[self listItemsTableView] selectedRow]];
    [[self managedObjectContext] deleteObject:itemToDelete];
    [[self listItemsTableView] setTrackingRow:-1];
    [[self listItemsTableView] setTrackingCell:nil];
}

-(IBAction)selectListFromEditListItemCell:(id)sender {
    RNListItem *selectedListItem = [[[self listItemsController] arrangedObjects] objectAtIndex:[[self listItemsTableView] selectedRow]];
    [[self listItemsTableView] setTrackingRow:-1];
    [[self listItemsTableView] setTrackingCell:nil];
    [sender setRepresentedObject:[selectedListItem list]];
    [NSApp sendAction:@selector(loadRootObjectFromCell:) to:nil from:sender];
    [self hideEditItemView];
}


#pragma mark -
#pragma mark List Item Table Delegate Methods

-(void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
    [aCell setRepresentedObject:[[[self listItemsController] arrangedObjects] objectAtIndex:rowIndex]];
}

- (NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation {
    NSAttributedString *titlestring = [cell attributedStringValue];
    NSRect titleRect = [cell titleRectForBounds:[aTableView rectOfRow:row]];
    if ([titlestring size].width <= NSWidth(titleRect))
        return nil;
    return [[[cell representedObject] list] title];
}

@end
